package org.shijiagen.utils;

import java.math.BigInteger;
import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;


/**
 * MD5加密工具类
 */
public abstract class MD5Util {

    /**
     * 将非空字符串转成md5
     * @param source
     * @return
     */
    public static String encode(String source){
        String result = null;
        if(source == null || "".equals(source)){
            throw new RuntimeException("加密字符串不可为空!");
        }
        MessageDigest instance = null;
        try {
            instance = MessageDigest.getInstance("MD5");
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
        instance.update(source.getBytes(StandardCharsets.UTF_8));
        result = new BigInteger(1,instance.digest()).toString(16).toUpperCase();
        return result;
    }
}
